extends CharacterBody2D

var dir = Vector2(1, 0)
var speed = 0
var size = 0
var char = null
@export var tutorial = false
@export var tuttype = 0

func _ready():
	char = get_node("/root/Main/Character")
	dir = (char.global_position - global_position).normalized()
	dir = dir.rotated(randf_range(-PI/2, PI/2))
	rotation = randf_range(-PI, PI)
	$CollisionShape2D.shape = RectangleShape2D.new()
	$CollisionShape2D.shape.size = Vector2(40, 64)
	var z = randi_range(1, 5)
	if z == 1 or z == 4 or z == 5:
		speed = randi_range(50, 300)
		size = randi_range(1, 3)
		$CollisionShape2D.shape.size *= (pow(1.5, size-1))
	elif z == 2:
		size = 4
		speed = 400
	elif z == 3:
		size = 5
		speed = 50
		$CollisionShape2D.shape.size *= (1.5*1.5)
	if tutorial == true:
		if tuttype < 4:
			z = 1
		else:
			z = 2
		size = tuttype
		speed = 0
	velocity = dir*speed
	if z == 1 or z == 4 or z == 5:
		$AnimatedSprite2D.animation = str(size)+str(randi_range(1,4))
	else:
		$AnimatedSprite2D.animation = str(size)
	
func collide(x):
	if "debris" in x.name:
		$AudioStreamPlayer2D.play()
	if "debris" in x.name or x.name == "Character" or "alien" in x.name:
		x.velocity = (x.global_position - global_position).normalized()*(6-size)*75
	
func _physics_process(delta: float) -> void:
	if move_and_slide() == true:
		var z = get_last_slide_collision().get_collider()
		z.collide(self)
		collide(z)
	if global_position.distance_to(char.global_position) > 4000:
		queue_free()
